//
//  DeviceInfoCommit.m
//  gdemm
//
//  Created by 乔明超 on 16/5/31.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "DeviceInfoCommit.h"
#import "HttpUtil.h"
#import "ToastManager.h"
#import "DeviceInfo.h"
#import "ApiSupport.h"
#import "KeyChain.h"
#import "RootChecker.h"
@implementation DeviceInfoCommit
+(instancetype) shareInstance
{
    static DeviceInfoCommit* _instance = nil;
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init] ;
    }) ;
    
    return _instance ;
}

-(void)commit{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setValue:[KeyChain getUDID] forKey:@"serial"];
    
//    [dict setValue:[KeyChain getUDID] forKey:@"mobileNum"];
//    NSLog(@"mobile = %@",[KeyChain getUDID]);
//    [dict setValue:[[UIDevice currentDevice] model] forKey:@"unitType"];
//    [dict setValue:[NSString stringWithFormat:@"%@%@",[[UIDevice currentDevice] systemName],[[UIDevice currentDevice] systemVersion]] forKey:@"OS"];
//    [dict setValue:[DeviceInfo getIpAddress] forKey:@"ip"];
//    [dict setValue:[DeviceInfo getCPU] forKey:@"cpu"];
//    [dict setValue:[DeviceInfo getScreenSize] forKey:@"screenSize"];
//    [dict setValue:[DeviceInfo getResolution] forKey:@"resolution"];
//    [dict setValue:[DeviceInfo getPriCamera] forKey:@"postPixel"];
//    [dict setValue:[DeviceInfo getSecCamera] forKey:@"frontPixel"];
//    [dict setValue:[NSString stringWithFormat:@"%@",[NSNumber numberWithBool:[[RootChecker getInstance]isRoot]]] forKey:@"rootState"];
//    [dict setValue:@"0" forKey:@"devState"];
    
    NSDictionary *response = [HttpUtil postWithUrl:[ApiSupport getDeviceInfo] withDict:dict withType:@"argv"];
    HRLog(@"设备信息 = %@ = %@",response,dict);
    if (response != nil) {
        [self parseResponse:response];
    }
}

-(void)parseResponse:(NSDictionary *)dict{
    NSString *status = [dict objectForKey:@"status"];
    
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    if ([status isEqualToString:@"success"]) {
        dispatch_async(dispatch_get_main_queue(), ^{

            [window makeToast:NSLocalizedString(@"str_device_regiset_success", @"设备注册成功")];
            
        });
        
    } else {
        NSString *errorCode = [dict objectForKey:@"reason"];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            switch ([errorCode intValue]) {
                case 1:
                    [window makeToast:NSLocalizedString(@"serial参数为空", @"")];
                    break;
                case 2:
                    [window makeToast:NSLocalizedString(@"设备注册成功，但需要管理员审批后才可以登入", @"")];
                    break;
                case 3:
                    [window makeToast:NSLocalizedString(@"设备注册功能已关闭或设备已禁用", @"")];
                    break;
                case 4:
                    [window makeToast:NSLocalizedString(@"等待设备注册或者设备已禁用", @"")];
                    break;
                case 5:
                    [window makeToast:NSLocalizedString(@"管理员拒绝设备审批通过", @"")];
                    break;
            }
        });
        
    }
    
//    NSString *result = [dict objectForKey:@"result"];
//    UIWindow *window = [UIApplication sharedApplication].delegate.window;
//    if ([result isEqualToString:@"success"]) {
//        BOOL needApprove = false;
//        NSString *regState = (NSString*)[dict objectForKey:@"regState"];
//        if ([regState isEqualToString:@"1"]) {
//            needApprove = true;
//        }
//        dispatch_async(dispatch_get_main_queue(), ^{
//            // 更新界面
//            if (needApprove) {
//                
//                [window makeToast:NSLocalizedString(@"str_needApprove", @"设备注册成功，但需要管理员审批后才可以登入")];
//                
//            } else {
//                [window makeToast:NSLocalizedString(@"str_device_regiset_success", @"设备注册成功")];
//            }
//        });
//        
//    } else {
//        NSString *errorCode = [dict objectForKey:@"errCode"];
//        dispatch_async(dispatch_get_main_queue(), ^{
//            
//            switch ([errorCode intValue]) {
//                case 0:
//                    [window makeToast:NSLocalizedString(@"str_softInfoErrorCode0", @"Json格式错误")];
//                case 1:
//                    [window makeToast:NSLocalizedString(@"str_softInfoErrorCode1", @"设备注册失败,未传入序列号或序列号错误")];
//                    break;
//                case 2:
//                    [window makeToast:NSLocalizedString(@"str_softInfoErrorCode2", @"设备注册失败,开发者状态参数错误")];
//                    break;
//                case 3:
//                    [window makeToast:NSLocalizedString(@"str_softInfoErrorCode3", @"设备注册失败,系统状态参数错误")];
//                    break;
//                case 4:
//                    [window makeToast:NSLocalizedString(@"str_softInfoErrorCode4", @"设备注册失败,已关闭注册功能")];
//                    break;
//                case 5:
//                    [window makeToast:NSLocalizedString(@"str_softInfoErrorCode5", @"注册失败，SQL错误")];
//                    break;
//            }
//        });
//        
//    }
}

@end
